package com.tc.infinitechat.authenticationservice.controller;

import com.tc.infinitechat.authenticationservice.common.Result;
import com.tc.infinitechat.authenticationservice.data.user.login.LoginRequest;
import com.tc.infinitechat.authenticationservice.data.user.login.LoginResponse;
import com.tc.infinitechat.authenticationservice.data.user.logincode.LoginCodeRequest;
import com.tc.infinitechat.authenticationservice.data.user.logincode.LoginCodeResponse;
import com.tc.infinitechat.authenticationservice.data.user.register.RegisterRequest;
import com.tc.infinitechat.authenticationservice.data.user.register.RegisterResponse;
import com.tc.infinitechat.authenticationservice.data.user.update.UpdateAvatarRequest;
import com.tc.infinitechat.authenticationservice.data.user.update.UpdateAvatarResponse;
import com.tc.infinitechat.authenticationservice.service.UserService;
import com.tc.infinitechat.authenticationservice.utils.jwt.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @ClassName UserController
 * @Author 大马鲛鱼丸Gaga
 * @Description TODO
 * @Date 2025/10/8 14:37
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result<RegisterResponse> register(@RequestBody RegisterRequest request){
        RegisterResponse registerResponse = userService.register(request);
        return Result.success(registerResponse);
    }

    @PostMapping("/login")
    public Result<LoginResponse> login(@RequestBody LoginRequest request){
        LoginResponse loginResponse = userService.login(request);
        return Result.success(loginResponse);
    }

    @PostMapping("/loginCode")
    public Result<LoginCodeResponse> loginCode(@RequestBody LoginCodeRequest request){
        LoginCodeResponse loginCodeResponse = userService.loginCode(request);
        return Result.success(loginCodeResponse);
    }

    @PutMapping("/avatar")
    public Result<UpdateAvatarResponse> updateAvatar(@Valid @RequestBody UpdateAvatarRequest request,
                                                     @RequestHeader String Authorization) {
        String id = JwtUtil.parse(Authorization).getSubject();
        UpdateAvatarResponse updateAvatarResponse = userService.updateAvatar(id, request);
        return Result.success(updateAvatarResponse);
    }

}
